
//Justin Adams
// Servlet used for games against AI
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.PrintWriter;
import java.io.File;
import java.util.ArrayList;
import tttCore.*;

@SuppressWarnings("serial")
public class TTTServlet extends HttpServlet{

	protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{
		//set up servlet
		response.setContentType("text/xml");
		PrintWriter out = response.getWriter();
		//get request
		String path = request.getPathInfo();
		System.out.println("path=" + path );
		BufferedReader rd = request.getReader();
		StringBuilder sb = new StringBuilder();
		//create the game manager used for coordinating AI moves
		GameManager GMan = new GameManager(path.substring(1));
		//process the current move
		GMan.gameProcess();

		int ch;
		while ( (ch=rd.read()) != -1 ) {
			sb.append( (char)ch);
		}
		System.out.println( sb.toString() );
		out.println( "<?xml version='1.0'?>" );
		// get the response from the GameManager and send it to the client
		out.println("<info>" + GMan.getResponseString() + "</info>");

	}
}

